home *** CD-ROM | disk | FTP | other *** search
- Path: linus.mitre.org!linus!rhl
- From: rhl@jambo.mitre.org (Roger Lincoln)
- Newsgroups: comp.lang.c++
- Subject: Compiling templates
- Date: 21 Apr 1996 01:02:34 GMT
- Organization: The MITRE Corporation
- Message-ID: <RHL.96Apr20210234@jambo.mitre.org>
- NNTP-Posting-Host: jambo.mitre.org
-
-
- I'm having trouble getting g++ to compile a templated class into a
- separate object module and link to it from another. Here's a simple
- sample, can anyone spot what I'm doing wrong?
-
-
- -- file: thing.hxx --
- template <class T>
- class Thing
- {
- public:
- Thing(T p);
- T GetThing();
- protected:
- T theThing;
- };
-
-
- -- file: thing.cxx --
- #include "thing.hxx"
-
- template <class T>
- Thing<T>::Thing(T p)
- {
- theThing = p;
- }
-
-
- template <class T>
- T Thing<T>::GetThing()
- {
- return (theThing);
- }
-
-
-
- -- file: test.cxx --
- #include <stream.h>
- #include "thing.hxx"
-
- main() {
- int i = 10;
- Thing<int> t(i);
- cout << "thing = " << t.GetThing();
- }
-
-
- -- file: Makefile --
- test: test.o thing.o
- g++ -o test test.o thing.o
-
- test.o: test.cxx thing.hxx
- g++ -c test.cxx
-
- thing.o: thing.cxx thing.hxx
- g++ -c thing.cxx
-
-
-
- Everything compiles fine, but when it tries to link it is unable to
- "find" the templated class...
-
-
- g++ -c test.cxx
- g++ -o test test.o thing.o
- Undefined first referenced
- symbol in file
- GetThing__t5Thing1Zi test.o
- __t5Thing1Zii test.o
- _._t5Thing1Zi test.o
- ld: fatal: Symbol referencing errors. No output written to test
- make: *** [test] Error 1
-
-
- I end up having to include the class implementation in any module that
- uses it, but this defeats the purpose of separate compilation units.
- Am I doing something wrong or does g++ not handle templates correctly?
-
- --
- \_____ \__ \__ \__ | Roger H. Lincoln | The activity of |
- \__ \__ \__ \__ \__ | MITRE Corp. | "debugging" ends when |
- \_____ \______ \__ | Bedford, MA | people get tired of |
- \__ \__ \__ \__ \_____ | rhl@mitre.org | doing it, not when the |
- \__ \__ \__ \__ \_____ | PGP key available | bugs are removed. |
-